package com.qmhl.toolposition.utils.umeng;

import android.util.Log;

import org.json.JSONObject;

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;

import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * Created by jessie.wangqq on 2019-11-22.
 */
public class HttpUtil {
    public static String postRequest(String url, JSONObject params) {
        String message = null;
        try {
            HttpURLConnection conn = postHttpsConnection(url, params);
            if (conn != null) {
                // 请求成功（相应码 == 200）
                int code = conn.getResponseCode();
                if (code == HttpURLConnection.HTTP_OK) {
                    // 取得该连接的输入流，以读取响应内容
                    InputStreamReader insr = new InputStreamReader(
                            conn.getInputStream());
                    StringBuffer strb = new StringBuffer();
                    int s;
                    while ((s = insr.read()) != -1) {
                        strb.append((char) s);
                    }
                    message = strb.toString();
                    Log.i("jessie", message);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

    private static HttpURLConnection postHttpsConnection(String urlStr, JSONObject params) {
        // 从上述SSLContext对象中得到SSLSocketFactory对象
        HttpURLConnection conn = null;
        try {
            URL myURL = new URL(urlStr);
            conn = (HttpURLConnection) myURL.openConnection();

//            // 创建SSLContext对象，并使用我们指定的信任管理器初始化
//            SSLContext sslContext = SSLContext.getInstance("TLS");
//            sslContext.init(null, new TrustManager[] { truseAllManager }, new SecureRandom());
//            SSLSocketFactory ssf = sslContext.getSocketFactory();

//            //设置加密协议
//            conn.setSSLSocketFactory(ssf);
            //设置请求方式
            conn.setRequestMethod("POST");
            //设置连接超时时长
            conn.setConnectTimeout(15000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setReadTimeout(15000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.connect();

            String outString = params.toString();

            OutputStream out = conn.getOutputStream();
            out.write(outString.getBytes());
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 加密协议
     */
    private static TrustManager truseAllManager = new X509TrustManager() {

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
}
